package cn.edu.zjgsu.ito.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.zjgsu.ito.dao.CourierOrderMapper;
import cn.edu.zjgsu.ito.dao.DemandOrderMapper;
import cn.edu.zjgsu.ito.dao.LatitudeLongitudeMapper;
import cn.edu.zjgsu.ito.dao.OrderNoCourierMapper;
import cn.edu.zjgsu.ito.dao.OrderTableMapper;
import cn.edu.zjgsu.ito.dao.PurchaseOrderMapper;
import cn.edu.zjgsu.ito.dao.UserAddressMapper;
import cn.edu.zjgsu.ito.dao.UserCourierMapper;
import cn.edu.zjgsu.ito.dao.UserInfomationMapper;
import cn.edu.zjgsu.ito.dao.UserLoginMapper;
import cn.edu.zjgsu.ito.model.CourierOrder;
import cn.edu.zjgsu.ito.model.CourierOrderExample;
import cn.edu.zjgsu.ito.model.DemandOrder;
import cn.edu.zjgsu.ito.model.DemandOrderExample;
import cn.edu.zjgsu.ito.model.LatitudeLongitude;
import cn.edu.zjgsu.ito.model.LatitudeLongitudeExample;
import cn.edu.zjgsu.ito.model.OrderNoCourier;
import cn.edu.zjgsu.ito.model.OrderNoCourierExample;
import cn.edu.zjgsu.ito.model.OrderTable;
import cn.edu.zjgsu.ito.model.PurchaseOrder;
import cn.edu.zjgsu.ito.model.UserAddress;
import cn.edu.zjgsu.ito.model.UserCourier;
import cn.edu.zjgsu.ito.model.UserCourierExample;
import cn.edu.zjgsu.ito.model.UserInfomation;
import cn.edu.zjgsu.ito.model.UserLogin;
import cn.edu.zjgsu.ito.service.UserCourierManageServiceI;
import cn.edu.zjgsu.ito.util.GetTime;
import cn.edu.zjgsu.ito.util.OrderStateConstant;
import cn.edu.zjgsu.ito.util.UserPowerStateConstant;
import cn.edu.zjgsu.ito.vo.CourierOrderMessageShowVo;
import cn.edu.zjgsu.ito.vo.CourierStateSaveVo;
import cn.edu.zjgsu.ito.vo.CourierStateShowVo;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.OrderNoCourierShowVo;
import cn.edu.zjgsu.ito.vo.UserCourierStateVo;
import cn.edu.zjgsu.ito.vo.UserPayCourierDepositVo;
import cn.edu.zjgsu.ito.vo.UserSubmitCourierSaveVo;

@Service("userCourierManageService")
public class UserCourierManageServiceImpl implements UserCourierManageServiceI {

	@Autowired
	private UserCourierMapper userCourierMapper;
	
	@Autowired
	private OrderNoCourierMapper orderNoCourierMapper;
	
	@Autowired
	private CourierOrderMapper courierOrderMapper;
	
	@Autowired
	private DemandOrderMapper demandOrderMapper;
	
	@Autowired
	private PurchaseOrderMapper purchaseOrderMapper;
	
	@Autowired
	private UserAddressMapper userAddressMapper;
	
	@Autowired
	private OrderTableMapper orderTableMapper;
	
	@Autowired
	private UserInfomationMapper userInfomationMapper;
	
	@Autowired
	private LatitudeLongitudeMapper latitudeLongitudeMapper;
	
	@Autowired
	private UserLoginMapper userLoginMapper;
	
	@Override
	public UserCourierStateVo requestAuditCourier(UserSubmitCourierSaveVo userSubmitCourierSaveVo,int userId) {

		UserCourierStateVo userCourierStateVo = new UserCourierStateVo();
		
		UserCourier userCourier = new UserCourier();
		
		BeanUtils.copyProperties(userSubmitCourierSaveVo, userCourier);
		userCourier.setUserId(userId);
		userCourier.setUserCourierState(UserPowerStateConstant.USER_COURIER_STATE_AUDIT);
		
		userCourierMapper.insertSelective(userCourier);
		/*if(courierFirstState(userSubmitCourierSaveVo.getUserId()) == 0){
			
			userCourier.setUserCourierState(UserPowerStateConstant.USER_COURIER_STATE_AUDIT);
			userCourierMapper.insert(userCourier);

		}else{
			userCourier.setUserCourierState(judgeIsCourier(userSubmitCourierSaveVo.getUserId()));
		}*/
		
		userCourierStateVo.setUserId(userCourier.getUserId());
		userCourierStateVo.setUserCourierState(userCourier.getUserCourierState());
		
		return userCourierStateVo;
		
	}

	@Override
	public UserCourierStateVo wantToBeCourier(int userId) {

		UserCourierStateVo userCourierStateVo = new UserCourierStateVo();
		
		userCourierStateVo.setUserId(userId);
		
		userCourierStateVo.setUserCourierState(judgeIsCourier(userId));
		
		return userCourierStateVo;
	}


	@Override
	public UserCourierStateVo payTheDeposit(
			UserPayCourierDepositVo userPayCourierDepositVo,int userId) {
		
		UserCourierStateVo userCourierStateVo = new UserCourierStateVo();
		
		/*int userCourierState = judgeIsCourier(userPayCourierDepositVo.getUserId());
		
		userCourierStateVo.setUserId(userPayCourierDepositVo.getUserId());
		
		userCourierStateVo.setUserCourierState(userCourierState);*/
		
		int money = Integer.valueOf(userPayCourierDepositVo.getMoney());
		if(money >= 300){
			UserCourier userCourier = new UserCourier();
			userCourier.setMoney(userPayCourierDepositVo.getMoney());
			userCourier.setUserCourierState(UserPowerStateConstant.USER_COURIER_STATE_ENABLE);
			UserCourierExample example = new UserCourierExample();
			example.or().andUserIdEqualTo(userId);
			userCourierMapper.updateByExampleSelective(userCourier, example);
			userCourierStateVo.setUserCourierState(UserPowerStateConstant.USER_COURIER_STATE_ENABLE);
			
			UserInfomation userInfomation = new UserInfomation();
			userInfomation.setUserId(userId);
			userInfomation.setPermissions(UserPowerStateConstant.USER_COURIER_STATE_ENABLE);
			userInfomationMapper.updateByPrimaryKeySelective(userInfomation);
			
		}else{
			userCourierStateVo.setUserCourierState(UserPowerStateConstant.USER_COURIER_STATE_NOPAYING);
		}
		
		return userCourierStateVo;
	}
	
	@Override
	public List<OrderNoCourierShowVo> courierHome() {
		List<OrderNoCourierShowVo> orderNoCourierShowVos = new ArrayList<OrderNoCourierShowVo>();
		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andOrderStateEqualTo(OrderStateConstant.ORDER_FIND_COURIER);
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper.selectByExample(example);
		System.out.println("订单个数" + orderNoCouriers.size());
		for(int i = 0;i < orderNoCouriers.size();i++){
			OrderNoCourierShowVo orderNoCourierShowVo = new OrderNoCourierShowVo();
			BeanUtils.copyProperties(orderNoCouriers.get(i), orderNoCourierShowVo);
			orderNoCourierShowVo.setAcceptTime(GetTime.dateToStr(orderNoCouriers.get(i).getAcceptTime()));
			orderNoCourierShowVo.setPurchaseFindCourierTime(GetTime.dateToStr(orderNoCouriers.get(i).getPurchaseFindCourierTime()));
			orderNoCourierShowVos.add(orderNoCourierShowVo);
		}
		
		return orderNoCourierShowVos;
	}
	
	@Override
	public List<OrderNoCourierShowVo> courierChooseHome(int temp) {
		List<OrderNoCourierShowVo> orderNoCourierShowVos = new ArrayList<OrderNoCourierShowVo>();
		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andOrderStateEqualTo(OrderStateConstant.ORDER_FIND_COURIER);
		if(temp == 0){//最新发布
			example.setOrderByClause("purchase_find_courier_time DESC");
		}
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper.selectByExample(example);
		System.out.println("订单个数" + orderNoCouriers.size());
		for(int i = 0;i < orderNoCouriers.size();i++){
			OrderNoCourierShowVo orderNoCourierShowVo = new OrderNoCourierShowVo();
			BeanUtils.copyProperties(orderNoCouriers.get(i), orderNoCourierShowVo);
			orderNoCourierShowVo.setAcceptTime(GetTime.dateToStr(orderNoCouriers.get(i).getAcceptTime()));
			orderNoCourierShowVo.setPurchaseFindCourierTime(GetTime.dateToStr(orderNoCouriers.get(i).getPurchaseFindCourierTime()));
			orderNoCourierShowVos.add(orderNoCourierShowVo);
		}
		
		return orderNoCourierShowVos;
	}
	
	@Override
	public List<OrderNoCourierShowVo> courierSelectHome(String temp) {
		List<OrderNoCourierShowVo> orderNoCourierShowVos = new ArrayList<OrderNoCourierShowVo>();
		OrderNoCourierExample example = new OrderNoCourierExample();
		example.or().andOrderStateEqualTo(OrderStateConstant.ORDER_FIND_COURIER).andAddressLike("%"+temp+"%");
		example.or().andOrderStateEqualTo(OrderStateConstant.ORDER_FIND_COURIER).andAddressDetailsLike("%"+temp+"%");
		example.or().andOrderStateEqualTo(OrderStateConstant.ORDER_FIND_COURIER).andPurchasePlaceLike("%"+temp+"%");
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper.selectByExample(example);
		System.out.println("订单个数" + orderNoCouriers.size());
		for(int i = 0;i < orderNoCouriers.size();i++){
			OrderNoCourierShowVo orderNoCourierShowVo = new OrderNoCourierShowVo();
			BeanUtils.copyProperties(orderNoCouriers.get(i), orderNoCourierShowVo);
			orderNoCourierShowVo.setAcceptTime(GetTime.dateToStr(orderNoCouriers.get(i).getAcceptTime()));
			orderNoCourierShowVo.setPurchaseFindCourierTime(GetTime.dateToStr(orderNoCouriers.get(i).getPurchaseFindCourierTime()));
			orderNoCourierShowVos.add(orderNoCourierShowVo);
		}
		
		return orderNoCourierShowVos;
	}
	
	@Override
	public int acceptOrder(OrderIdVo orderIdVo,int userId) {
		
		CourierOrderExample example = new CourierOrderExample();
		example.or().andOrderIdEqualTo(orderIdVo.getOrderId());
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(example);
		if(courierOrders.size() > 0){
			return 0;
		}
		
		CourierOrder courierOrder = new CourierOrder();
		courierOrder.setOrderId(orderIdVo.getOrderId());
		courierOrder.setCourierAcceptTime(GetTime.getNowTime());
		courierOrder.setCourierId(userId);
		courierOrder.setOrderCourierState(OrderStateConstant.COURIER_TAKEING);
		courierOrderMapper.insertSelective(courierOrder);
		
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderIdVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_WAIT_SEND);
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
		
		return 1;
	}
	
	@Override
	public List<CourierOrderMessageShowVo> courierOwnOrder(int userId) {
		List<CourierOrderMessageShowVo> courierOrderMessageShowVos = new ArrayList<CourierOrderMessageShowVo>();
		CourierOrderExample courierOrderExample = new CourierOrderExample();
		courierOrderExample.or().andCourierIdEqualTo(userId);
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(courierOrderExample);
		
		for(int i = 0;i < courierOrders.size();i++){
			OrderTable orderTable = orderTableMapper.selectByPrimaryKey(courierOrders.get(i).getOrderId());
			CourierOrderMessageShowVo courierOrderMessageShowVo = new CourierOrderMessageShowVo();
			BeanUtils.copyProperties(orderTable, courierOrderMessageShowVo);
			copyCourierMessage(courierOrderMessageShowVo,courierOrders.get(i).getOrderId());
			copyDemandMessage(courierOrderMessageShowVo,orderTable.getDemandId());
			copyPurchaseMessage(courierOrderMessageShowVo,orderTable.getPurchaseId());
			copyLatitudeLongitude(courierOrderMessageShowVo,courierOrders.get(i).getOrderId());
			courierOrderMessageShowVos.add(courierOrderMessageShowVo);
		}
		
		return courierOrderMessageShowVos;
	}
	
	@Override
	public List<CourierOrderMessageShowVo> courierSelectOwnOrder(String temp,int userId) {
		List<CourierOrderMessageShowVo> courierOrderMessageShowVos = new ArrayList<CourierOrderMessageShowVo>();
		CourierOrderExample courierOrderExample = new CourierOrderExample();
		courierOrderExample.or().andCourierIdEqualTo(userId);
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(courierOrderExample);
		
		for(int i = 0;i < courierOrders.size();i++){
			OrderTable orderTable = orderTableMapper.selectByPrimaryKey(courierOrders.get(i).getOrderId());
			CourierOrderMessageShowVo courierOrderMessageShowVo = new CourierOrderMessageShowVo();
			BeanUtils.copyProperties(orderTable, courierOrderMessageShowVo);
			copyCourierMessage(courierOrderMessageShowVo,courierOrders.get(i).getOrderId());
			copyDemandMessage(courierOrderMessageShowVo,orderTable.getDemandId());
			copyPurchaseMessage(courierOrderMessageShowVo,orderTable.getPurchaseId());
			copyLatitudeLongitude(courierOrderMessageShowVo,courierOrders.get(i).getOrderId());
			
			String selectStr = courierOrderMessageShowVos.get(i).getAddress()+courierOrderMessageShowVos.get(i).getAddressDetails()+
					courierOrderMessageShowVos.get(i).getPurchaseAddress();
			if(selectStr.contains(temp)){
				courierOrderMessageShowVos.add(courierOrderMessageShowVo);
			}
		}
		
		return courierOrderMessageShowVos;
	}
	
	@Override
	public CourierOrderMessageShowVo courierOrderMessage(OrderIdVo orderIdVo) {

		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderIdVo.getOrderId());
		CourierOrderMessageShowVo courierOrderMessageShowVo = new CourierOrderMessageShowVo();
		courierOrderMessageShowVo.setOrderState(orderTable.getOrderState());
		copyCourierMessage(courierOrderMessageShowVo,orderIdVo.getOrderId());
		copyDemandMessage(courierOrderMessageShowVo,orderTable.getDemandId());
		copyPurchaseMessage(courierOrderMessageShowVo,orderTable.getPurchaseId());
		copyLatitudeLongitude(courierOrderMessageShowVo,orderIdVo.getOrderId());
	
		return courierOrderMessageShowVo;
	}
	
	
	@Override
	public CourierStateShowVo courierChangeState(
			CourierStateSaveVo courierStateSaveVo) {
		
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(courierStateSaveVo.getOrderId());
		
		CourierStateShowVo courierStateShowVo = new CourierStateShowVo();
		
		CourierOrder courierOrder = new CourierOrder();
		courierOrder.setOrderId(courierStateSaveVo.getOrderId());
		if(courierStateSaveVo.getOrderCourierState() == OrderStateConstant.COURIER_SENDING){
			courierOrder.setOrderCourierState(OrderStateConstant.COURIER_SENDING);
			
			orderTable.setOrderState(OrderStateConstant.ORDER_SENDING);
			orderTableMapper.updateByPrimaryKeySelective(orderTable);
			
		}else if(courierStateSaveVo.getOrderCourierState() == OrderStateConstant.COURIER_ENDING){
			courierOrder.setOrderCourierState(OrderStateConstant.COURIER_ENDING);
			courierOrder.setArriveTime(GetTime.getNowTime());
			orderTableMapper.updateByPrimaryKeySelective(orderTable);
		}
		
		
		
		CourierOrderExample example = new CourierOrderExample();
		example.or().andOrderIdEqualTo(courierStateSaveVo.getOrderId());
		courierOrderMapper.updateByExampleSelective(courierOrder, example);
			
		courierStateShowVo.setOrderCourierState(courierOrder.getOrderCourierState());
		
		return courierStateShowVo;
	}
	
	
	/**
	 * 判断快递员状态
	 * @param userId
	 * @return
	 */
	public int judgeIsCourier(int userId){
		
		UserCourierExample example = new UserCourierExample();
		example.or().andUserIdEqualTo(userId);
		List<UserCourier> userCouriers = userCourierMapper.selectByExample(example);
		if(userCouriers.size() == 0 ){//不是快递员
			return UserPowerStateConstant.USER_COURIER_STATE_DISABLE;
		}else{
			return userCouriers.get(0).getUserCourierState();
		}
	}
	
	/**
	 * 是否第一次申请快递员
	 * @param userId
	 * @return
	 */
	public int courierFirstState(int userId){
		
		UserCourierExample example = new UserCourierExample();
		example.or().andUserIdEqualTo(userId);
		List<UserCourier> userCouriers = userCourierMapper.selectByExample(example);
		if(userCouriers.size() == 0 ){//第一次申请
			return 0;
		}
		
		return 1;
	}

	/**
	 * 复制快递员表
	 * @param courierOrderMessageShowVo
	 * @param orderId
	 * @return
	 */
	public CourierOrderMessageShowVo copyCourierMessage(CourierOrderMessageShowVo courierOrderMessageShowVo,int orderId){
		CourierOrderExample example = new CourierOrderExample();
		example.or().andOrderIdEqualTo(orderId);
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(example);
		BeanUtils.copyProperties(courierOrders.get(0), courierOrderMessageShowVo);
		courierOrderMessageShowVo.setCourierAcceptTime(GetTime.dateToStr(courierOrders.get(0).getCourierAcceptTime()));
		if(courierOrders.get(0).getArriveTime() != null){
			courierOrderMessageShowVo.setArriveTime(GetTime.dateToStr(courierOrders.get(0).getArriveTime()));
		}
		return courierOrderMessageShowVo;
	}
	
	/**
	 * 复制求购单
	 * @param courierOrderMessageShowVo
	 * @param orderId
	 * @return
	 */
	public CourierOrderMessageShowVo copyDemandMessage(CourierOrderMessageShowVo courierOrderMessageShowVo,int demandId){
		
		DemandOrderExample demandOrderExample = new DemandOrderExample();
		demandOrderExample.or().andDemandIdEqualTo(demandId);
		List<DemandOrder> demandOrders = demandOrderMapper.selectByExample(demandOrderExample);
		BeanUtils.copyProperties(demandOrders.get(0), courierOrderMessageShowVo);
		courierOrderMessageShowVo.setExpectedArrivalTime(GetTime.dateToStr(demandOrders.get(0).getExpectedArrivalTime()));
		courierOrderMessageShowVo.setDemandCreatetime(GetTime.dateToStr(demandOrders.get(0).getDemandCreatetime()));
		
		UserAddress userAddress = userAddressMapper.selectByPrimaryKey(demandOrders.get(0).getAddressId());
		BeanUtils.copyProperties(userAddress, courierOrderMessageShowVo);
		
		return courierOrderMessageShowVo;
	}
	
	/**
	 * 复制代购单
	 * @param courierOrderMessageShowVo
	 * @param demandId
	 * @return
	 */
	public CourierOrderMessageShowVo copyPurchaseMessage(CourierOrderMessageShowVo courierOrderMessageShowVo,int purchaseId){
		
		PurchaseOrder purchaseOrder = purchaseOrderMapper.selectByPrimaryKey(purchaseId);
		UserInfomation userInfomation = userInfomationMapper.selectByPrimaryKey(purchaseOrder.getPurchaseUserId());
		BeanUtils.copyProperties(purchaseOrder, courierOrderMessageShowVo);
		courierOrderMessageShowVo.setAcceptTime(GetTime.dateToStr(purchaseOrder.getAcceptTime()));
		courierOrderMessageShowVo.setPurchaseName(userInfomation.getNickname());
		
		int userId = userInfomation.getUserId();
		UserLogin userLogin = userLoginMapper.selectByPrimaryKey(userId);
		
		courierOrderMessageShowVo.setPurchasePhone(userLogin.getUsername());//存电话
		return courierOrderMessageShowVo;
	}
	
	/**
	 * 复制经纬度
	 * @param courierOrderMessageShowVo
	 * @param orderId
	 * @return
	 */
	public CourierOrderMessageShowVo copyLatitudeLongitude(CourierOrderMessageShowVo courierOrderMessageShowVo,int orderId){
		LatitudeLongitudeExample example = new LatitudeLongitudeExample();
		example.or().andOrderIdEqualTo(orderId);
		List<LatitudeLongitude> latitudeLongitude = latitudeLongitudeMapper.selectByExample(example);
		
		if(latitudeLongitude.size() > 0){
			BeanUtils.copyProperties(latitudeLongitude.get(0), courierOrderMessageShowVo);
		}
		return courierOrderMessageShowVo;
	}

}
